package ch.ffhs.radarpp.model;

import java.io.Serializable;

import org.json.JSONException;
import org.json.JSONObject;

import ch.ffhs.radarpp.R;

import android.content.Context;


public class Friend implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private String phone;
	private boolean isAccepted;
	private boolean isWaiting; 
	private int userid;
	private FriendsLocation lastLocation;
	private boolean isADummy;
	private boolean isLoading;
	
	public Friend()
	{
		
	}
	
	public Friend(JSONObject jsonFriend, Context context) throws JSONException
	{
		this.name = jsonFriend.getString(context.getString(R.string.friend_username));
		this.phone = jsonFriend.getString(context.getString(R.string.friend_phone));
		this.isAccepted = jsonFriend.getBoolean(context.getString(R.string.friend_isaccepted));
		this.userid = jsonFriend.getInt(context.getString(R.string.friend_userid));
		this.isWaiting = jsonFriend.getBoolean(context.getString(R.string.friend_iswaiting));
		
		JSONObject lastLocation = jsonFriend.optJSONObject(context.getString(R.string.friend_lastLocation));
		
		if(lastLocation != null)
		{
			this.lastLocation = new FriendsLocation(lastLocation, context);
		}
		
	}
	
	public Friend(boolean isADummy, boolean isLoading) {
		this.isADummy = isADummy;
		this.setLoading(isLoading);
	}

	public FriendsLocation lastLocation()
	{
		return lastLocation;
	}
	

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public boolean getIsAccepted(){
		return isAccepted;
	}

	public int getUserid() {
		return userid;
	}
	
	public boolean getIsWaiting()
	{
		return isWaiting;
	}

	public boolean getIsADummy() {
		return this.isADummy;
	}

	public boolean isLoading() {
		return isLoading;
	}

	public void setLoading(boolean isLoading) {
		this.isLoading = isLoading;
	}

	public int compareTo(Friend rhs, double currentLat, double currentLon) {
		if(lastLocation() == null)
		{
			return 1;
		}else if(rhs.lastLocation() == null)
		{
			return -1;
		}else{
			return (int) (FriendsLocation.calcDistance(lastLocation().getLatitude(), lastLocation().getLongitude(), currentLat, currentLon) -
					FriendsLocation.calcDistance(rhs.lastLocation().getLatitude(), rhs.lastLocation().getLongitude(), currentLat, currentLon));
		}
	}
	

}
